package com.haha.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ToastActivity extends AppCompatActivity {
    private Button btn_toast_1;
    private Button btn_toast_2;
    private Button btn_toast_3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);
        btn_toast_1=(Button) findViewById(R.id.btn_toast_1);
        btn_toast_2=(Button) findViewById(R.id.btn_toast_2);
        btn_toast_3=(Button) findViewById(R.id.btn_toast_3);

        OnClick onClick = new OnClick();
        btn_toast_1.setOnClickListener(onClick);
        btn_toast_2.setOnClickListener(onClick);
        btn_toast_3.setOnClickListener(onClick);

    }

    class OnClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_toast_1:
                    Toast.makeText(getApplicationContext(),"Toast显示",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn_toast_2:
                    Toast toast = Toast.makeText(getApplicationContext(), "Toast居中显示", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER,0,0);
                    toast.show();
                    break;
                case R.id.btn_toast_3:
                    Toast imageToast = new Toast(getApplicationContext());
                    //创建一个布局填充器
                    LayoutInflater inflater = LayoutInflater.from(ToastActivity.this);
                    //布局填充器使用layout_toast进行填充
                    View view = inflater.inflate(R.layout.layout_toast, null);
                    //设置view里边的控件效果
                    ImageView imageView = (ImageView)view.findViewById(R.id.iv_toast);
                    TextView textView = (TextView)view.findViewById(R.id.tv_toast);
                    //设置图片和文本
                    imageView.setImageResource(R.drawable.bg_fgo);
                    textView.setText("自定义的Toast");
                    //将view存入toast
                    imageToast.setView(view);
                    imageToast.show();
                    break;
            }

        }
    }
}